$(function () {
    // 接受id
//    console.log(location.href.split("="));
//    console.log(location.href.split("=")[1]);
   function initForm() {
       let id =location.href.split("=")[1];
    //    console.log(id);
    axios({
        url: '/my/article/cates'
    }).then(({
        data: res
    }) => {
        // console.log(res);
        if (res.status !== 0) {
            return layer.msg(res.message)
            }
            console.log(res);
            form.val("formEdit", res.data)
            // console.log(res.data.content)
            setTimeout(() => {
                tinyMCE.activeEditor.setContent(res.data.content)
            }, 500);

            let baseURL = 'http://api-breakingnews-web.itheima.net'
            let url = baseURL + res.data.cover_img
            img
                .cropper('destroy') // 销毁旧的裁剪区域
                .attr('src', url) // 重新设置图片路径
                .cropper(options) // 重新初始化裁剪区域
        })
    }
   
    let layer = layui.layer
    let form = layui.form
    initArtCateList()

    // 需求1
    function initArtCateList() {
        axios({
            url: '/my/article/cates'
        }).then(({
            data: res
        }) => {
            // console.log(res);
            if (res.status !== 0) {
                return layer.msg(res.message)
            }
            let arr = [`<option value="">请选择文章所属分类</option>`]
            res.data.forEach(ele => {
                if (ele.Id < 0) return
                arr.push( ` <option value="${ele.Id}">${ele.name}</option>`)
            })
            let str = arr.join('')
            $("[name=cate_id]").empty().html(str)
            form.render()
            initForm()
        })
    }
    initEditor()
    // 1. 初始化图片裁剪器
    var img = $('#image')

    // 2. 裁剪选项
    var options = {
        aspectRatio: 400 / 280,
        preview: '.img-preview'
    }

    // 3. 初始化裁剪区域
    img.cropper(options)

    // 4文章发布
    $("#formPub").on('submit', function (e) {
        e.preventDefault()
        let fd = new FormData(this)
        fd.append('state', state)
        console.log(...fd);
        img.cropper('getCroppedCanvas', {
                width: 400,
                height: 200
            })
            .toBlob(function (blob) {
                fd.append("cover_img", blob)
                console.log(...fd);
                axios({
                    method: 'post',
                    url: '/my/article/edit',
                    data: fd
                }).then(({
                    data: res
                }) => {
                    if (res.status !== 0) {
                        return layer.msg(res.message)
                    }
                    layer.msg("恭喜您,修改文章成功")
                })
            })
    })

    // 需求5:点击按钮,赋值状态
    let state = ''
    $("#btn1").on('click', function () {
        state = '已发布'
    })
    $("#btn2").on('click', function () {
        state = '草稿'
    })

    //需求6: 点击按钮,触发input事件
    $("#chooseImageBtn").on("click", function () {
        $("#chooseImageInp").click()
    })

    // 需求5.选择照片 渲染照片
    $("#chooseImageInp").on('change', function (e) {

        var file = e.target.files[0]
        if (file == undefined) {
            return
        }
        let url = URL.createObjectURL(file)

        img
            .cropper('destroy') // 销毁旧的裁剪区域
            .attr('src', url) // 重新设置图片路径
            .cropper(options) // 重新初始化裁剪区域
    })


})